package com.qcby.controller;

import com.qcby.entity.PageResult;
import com.qcby.entity.Query.ShopQuery;
import com.qcby.entity.Query.UserQuery;
import com.qcby.entity.Result;
import com.qcby.entity.Shop;
import com.qcby.entity.User;
import com.qcby.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/getLatestUserId")
    public Integer getLatestUserId() {
        // SQL：select max(id) from user; （获取最大ID，即最近插入的用户ID）
        return userService.selectMaxId();
    }
    @RequestMapping("/list")
    public Result list(UserQuery userQuery){
        PageResult<User> list = userService.list(userQuery);
        return Result.success(list);
    }
    @RequestMapping("deleteById")
    public Result delete(Integer id){
        userService.deleteById(id);
        return Result.success();
    }
    @RequestMapping("/findById")
    public Result findById(Integer id){
        User user = userService.findById(id);
        return Result.success(user);
    }
    @RequestMapping("/updateById")
    public Result updateById(User user){
        userService.updateById(user);
        return Result.success();
    }
    @RequestMapping("/add")
    public Result add(User user){
        System.out.println(user);
        userService.add(user);
        return Result.success();
    }
    @RequestMapping("/login")
    @ResponseBody
    public Result userLogin(String username, String password, HttpSession session){
        List<User> i=userService.UserLogin(username,password);
        System.out.println(i);
        System.out.println(i.size());

        if(i.size()==1){
            User user = i.get(0);
            System.out.println(user);
            session.setAttribute("currentUser",user);
            return Result.success(user);
        }

        else
            return Result.error("登录失败");
    }
    @PostMapping("/register")
    @ResponseBody
    public Result<String> register(User user){
        int i=userService.register(user);
        if(i==1)
            return Result.success("注册成功");
        else
            return Result.error("注册失败");
    }
    @PostMapping ("/getCurrentUser")
    @ResponseBody
    public Result<User> getCurrentUser(HttpSession session) {
        // 从Session中获取登录时存入的用户信息
        User currentUser = (User) session.getAttribute("currentUser");
        //User currentUser=userService.findById(1);
        if (currentUser != null) {
            return  Result.success(currentUser);
        } else {
            return Result.error("获取用户信息失败");
        }
    }
    @PostMapping("/isOwner")
    @ResponseBody
    public Result<String> isOwner(User user){

        if(user.getIsOwner()==1){
            return Result.success("您已经是店主了");
        }else if(user.getIsOwner()==0) {
            int i=userService.isOwner(user.getId());
            if(i==1)
                return Result.success("成为店主！");
            else
                return Result.error("后台出错！");
        }else {
            return Result.error("数据库出错！！");
        }

    }
    @RequestMapping("/findByName")
    public Result findByName(String username){
        User user = userService.findByName(username);
        return Result.success(user);
    }
    @RequestMapping("/updatePassword")
    public Result updatePassword(@RequestParam("password")String password,@RequestParam("account") String account,@RequestParam("newPassword") String newPassword){
        Result result = userService.updatePassword(password,account,newPassword);
        return result;
    }
    @RequestMapping("/updateUser")
    public Result updateUser(User user){
        userService.updateById(user);
        return Result.success();
    }
}
